home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / faq / wdj0497.zip / HEAPOLE.ZIP / ALLOC.H < prev    next >
C/C++ Source or Header  |  1997-02-03  |  3KB  |  89 lines

  1. /* alloc.h - control which allocator is used.
  2.  *
  3.  * This header file is included by programs that want to test
  4.  * against various memory allocators. Such programs will call
  5.  * the macros MALLOC() and FREE() rather than a specific allocator.
  6.  * This header file expands those macros based on how you set
  7.  * two command-line options:
  8.  * 
  9.  * /DALLOCATOR=1   <- use compiler's standard malloc()/free()
  10.  * /DALLOCATOR=2   <- use compiler's standard new/delete
  11.  * /DALLOCATOR=3   <- use OLE's IMalloc() interface
  12.  * /DALLOCATOR=4   <- use Win32's Heap management functions
  13.  *
  14.  * To use this header file, you simply #include it and then make
  15.  * sure to call AllocInit() before performing any calls to MALLOC()
  16.  * and FREE(). A HEAPMIN() function is also provided, though it
  17.  * expands to -1 (failure) for those environments that don't support
  18.  * it. You must also link with alloc.obj, which contains AllocInit().
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include <objbase.h>    /* for IMalloc interface */
  23. #include <windows.h>
  24.  
  25. int     AllocInit(void);
  26. DWORD   MemStats(DWORD*);
  27. DWORD   MemAvail(void);
  28. void*   GetMem(DWORD Size);
  29.  
  30. void    DummyFree(void* Foo);
  31. void*   DummyMalloc(size_t Size);
  32.  
  33. extern  HANDLE      MyHeap;
  34. extern  IMalloc*    OleAlloc;
  35.  
  36. #if !defined(ALLOCATOR)
  37. #   error   Set ALLOCATOR to a value defined in alloc.h
  38. #elif ALLOCATOR==1
  39. #   define MALLOC_NAME      "malloc()"
  40. #   define MALLOC           malloc
  41. #   define FREE             free
  42. #   if defined(__BORLANDC__)    /* Borland doesn't need a heapmin() */
  43. #       define HEAPMIN()        (0)
  44. #   elif defined(__WATCOMC__)   /* Watcom uses a different name     */
  45. #       define HEAPMIN()        (_heapshrink())
  46. #   else
  47. #       define HEAPMIN()        (_heapmin())
  48. #   endif
  49. #elif ALLOCATOR==2
  50. #   define MALLOC_NAME      "operator new"
  51. #   define MALLOC(size)     ((void*)new char[size])
  52. #   define FREE(p)          (delete[] p)
  53. #   define HEAPMIN()        (_heapmin())
  54. #elif ALLOCATOR==3
  55. #   define MALLOC_NAME      "OLE IMalloc"
  56. #   if defined __cplusplus
  57. #       define MALLOC(size) OleAlloc->Alloc(size)
  58. #       define FREE(p)      OleAlloc->Free(p)
  59. #       define HEAPMIN()    (OleAlloc->HeapMinimize(), 0)
  60. #   else
  61. #       define MALLOC(size) OleAlloc->lpVtbl->Alloc(OleAlloc, size)
  62. #       define FREE(p)      OleAlloc->lpVtbl->Free(OleAlloc, p)
  63. #       define HEAPMIN()    (OleAlloc->lpVtbl->HeapMinimize(OleAlloc), 0)
  64. #   endif
  65. #elif ALLOCATOR==4
  66. #   define MALLOC_NAME      "Win32 HeapAlloc()"
  67. #   define MALLOC(size)     (HeapAlloc(MyHeap, \
  68.                                 HEAP_NO_SERIALIZE, size))
  69. #   define FREE(p)          (HeapFree(MyHeap, HEAP_NO_SERIALIZE, p))
  70. #   define HEAPMIN()        (HeapCompact(MyHeap, HEAP_NO_SERIALIZE))
  71. #else
  72. #   error   Set ALLOCATOR to a value defined in alloc.h
  73. #endif
  74.  
  75.  
  76.  
  77. #if defined(__BORLANDC__)
  78. #    define COMPILER_NAME   "Borland"
  79. #elif defined(__WATCOMC__)
  80. #    define COMPILER_NAME   "Watcom"
  81. #elif defined(__SC__)
  82. #    define COMPILER_NAME   "Symantec"
  83. #elif defined(_MSC_)
  84. #    define COMPILER_NAME   "Microsoft"
  85. #else
  86. #    define COMPILER_NAME   "Unknown"
  87. #endif
  88.  
  89.